package com.example.active.sf;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/17 8:35
 */
public class ProducerTopic
{
    static ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.0.118:61616");

    public static void main(String[] args)
    {
        try
        {
            Connection connection = factory.createConnection();

            //第一个是事务，第二个是签收
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //创建目的地
            Topic topic01 = session.createTopic("topic01");
            //创建消息生产者
            MessageProducer producer = session.createProducer(topic01);
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            connection.start();
            for (int i = 0; i < 3; i++)
            {
                //创建消息
                TextMessage textMessage = session.createTextMessage("topic01-->" + i);
                producer.send(textMessage);
            }
            producer.close();
            session.close();
            connection.close();
            System.out.println("topic01消息发布到MQ");
        } catch (JMSException e)
        {
            e.printStackTrace();
        }
    }
}
